package com.medical.acc.web.controller;

import com.medical.common.JsonResult;
import com.medical.acc.model.Level;
import com.medical.acc.service.LevelService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

//资源管理
@RestController
@RequestMapping("/level")
public class LevelController {
    @Autowired
    private LevelService levelService;

    //返回Level总信息的json数组
    @ApiOperation(value="返回Level总信息的json数组", notes="返回Level总信息的json数组")
    @RequestMapping(value="", method=RequestMethod.GET)
    public JsonResult selectAllLevel() {
        return new JsonResult(levelService.selectAllLevel());
    }


    //根据id返回对象
    @ApiOperation(value="根据id返回对象", notes="根据id返回对象")
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getById(@PathVariable int id){
        return new JsonResult("200","",levelService.selectByPrimaryKey(id));
    }

    /**
     *
     * @param name
     * @param description
     * @return
     */
    //新增
    @ApiOperation(value="新增", notes="新增")
    @RequestMapping(value = "/postlevel",method = RequestMethod.POST)
    public JsonResult saveLevel(@RequestParam(value = "name",required = false) String name,
                                @RequestParam(value = "description",required = false) String description){
        Level level=new Level();
        level.setName(name);
        level.setDescription(description);
        try {
            levelService.saveLevel(level);
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
        return new JsonResult("200","",levelService.selectByName(name));
    }

    /**
     *
     * @param id
     * @param name
     * @param description
     * @return
     */
    //编辑
    @ApiOperation(value="编辑", notes="编辑")
    @RequestMapping(value = "/patchlevel/{id}",method = RequestMethod.POST)
    public JsonResult patchLevel(@PathVariable int id,
                                 @RequestParam(value = "name",required = false) String name,
                                 @RequestParam(value = "description",required = false) String description){
        Level level=levelService.selectByPrimaryKey(id);
        level.setName(name);
        level.setDescription(description);
        try{
        levelService.updateByPrimaryKey(level);
        return new JsonResult("200",levelService.selectByName(name));
        }catch (Exception e){

            return new JsonResult("500",e.getMessage());
        }
        }


    //删除
    @ApiOperation(value="删除", notes="删除")
    @RequestMapping(value="/deletelevel/{id}",method = RequestMethod.POST )
    public JsonResult deleteLevel(@PathVariable  int id){
        try{
        levelService.deleteByPrimaryKey(id);
        return new JsonResult("200","删除成功");
        }catch (Exception e){

            return new JsonResult("500",e.getMessage());
        }
        }

}